package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"zg3/internal/api/request"
	"zg3/internal/api/response"
	"zg3/internal/models/service"
)

func GroupCreate(c *gin.Context) {
	var json request.GroupCreate
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	check := service.GroupCheck(&json, userId)

	if check != "" {
		response.Response(c, 400, check, nil)
		return
	}

	group := service.CreateGroup(&json, userId)

	if group != "" {
		response.Response(c, 400, group, nil)
		return
	}

	response.Response(c, 200, "群聊创建成功", nil)

}

func CreateDev(c *gin.Context) {
	var json request.CreateDev
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	dev := service.CreateDev(&json, userId)

	if dev != "" {
		response.Response(c, 400, dev, nil)
		return
	}

	response.Response(c, 200, "管理员设置成功", nil)

}

func CreateGroupMember(c *gin.Context) {
	var json request.CreateGroupMember
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	member := service.CreateGroupMember(&json, userId)

	if member != "" {
		response.Response(c, 400, member, nil)
		return
	}

	response.Response(c, 200, "群成员邀请成功", nil)

}

func DeleteMember(c *gin.Context) {
	var json request.DeleteMember
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	member := service.DeleteMember(&json, userId)

	if member != "" {
		response.Response(c, 400, member, nil)
		return
	}

	response.Response(c, 200, "群成员移除成功", nil)

}

func Close(c *gin.Context) {
	var json request.Close
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	s := service.Close(&json, userId)

	if s != "" {
		response.Response(c, 400, s, nil)
		return
	}

	response.Response(c, 200, "解散群成功", nil)
}

func GiveYou(c *gin.Context) {
	var json request.GiveYou
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	you := service.GiveYou(&json, userId)
	if you != "" {
		response.Response(c, 400, you, nil)
		return
	}

	response.Response(c, 200, "群主转让成功", nil)
}

func GroupList(c *gin.Context) {
	var json request.GroupList
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	list := service.GroupList(&json, userId)

	response.Response(c, 200, "群展示成功", list)
}

func GroupTop(c *gin.Context) {
	var json request.GroupTop
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	service.GroupTop(&json, userId)

	response.Response(c, 200, "群置顶成功", nil)
}

func GroupNoTop(c *gin.Context) {
	var json request.GroupTop
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	service.GroupNoTop(&json, userId)

	response.Response(c, 200, "群取消置顶成功", nil)
}

func CreateRemark(c *gin.Context) {
	var json request.CreateRemark
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	remark := service.CreateRemark(&json, userId)

	if remark != "" {
		response.Response(c, 400, remark, nil)
	}

	response.Response(c, 200, "公告发送成功", nil)

}
